//
//  IdeaScaleAppDelegate.m
//  IdeaScale
//
//  Created by Jeremy Przasnyski on 10/30/09.
//  Copyright Cavoort, LLC 2009. All rights reserved.
//

#import "IdeaScaleAppDelegate.h"
#import "ISRootViewController.h"
#import "ISFeedback.h"
#import "IdeaScaleConstants.h"

@implementation IdeaScaleAppDelegate

@synthesize window = _window;
@synthesize navigationController = _navigationController;

#pragma mark UIApplicationDelegate

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
	if (LOG) {NSLog(@"%s , %@",__FUNCTION__, launchOptions);}
	[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
	[_window addSubview:[_navigationController view]];
    [_window makeKeyAndVisible];
	[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert)];
	[ISFeedback initSharedInstance:kISServiceExampleAPIKey];
	
	if (launchOptions && [[ISFeedback sharedInstance] isFeedbackPushNotification:launchOptions]) {
		[[ISFeedback sharedInstance] handleFeedbackPushNotification:launchOptions 
												   inViewController:[_navigationController topViewController]
															 prompt:NO];
		return YES;
	} else {
		// This app can only handle nil options or feedback options.
		return launchOptions == nil;
	}
}

-(void)application:(UIApplication*)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)token {
	if (LOG) {NSLog(@"%s",__FUNCTION__);}
	[[ISFeedback sharedInstance] registerForPushNotifications:token];
	//TODO: Register for other provider push notifications.
}

-(void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSLog(@"Error in registration. Error: %@", err);
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
	NSLog(@"RECEIVED MESSAGE: %@", userInfo);
	if ([[ISFeedback sharedInstance] isFeedbackPushNotification:userInfo]) {
		[[ISFeedback sharedInstance] handleFeedbackPushNotification:userInfo 
												   inViewController:[_navigationController topViewController]
															 prompt:YES];
	}
}

- (void)applicationWillTerminate:(UIApplication *)application { }

#pragma mark NSObject

- (void)dealloc {
	[_navigationController release];
	[_window release];
	[super dealloc];
}

@end

